//
//  NBHomeTableViewCell.swift
//  NBSinaWeibo
//
//  Created by ac on 16/5/24.
//  Copyright © 2016年 ac. All rights reserved.
//

import UIKit
import SnapKit

class NBHomeTableViewCell: UITableViewCell {
    
    private lazy var originView:NBStatusOriginView = NBStatusOriginView()
    private lazy var toolBarView:NBStatusToolBar = NBStatusToolBar()
    private lazy var resweetedView:NBStatusRetweetedView = NBStatusRetweetedView()
    private var toolBarTopConstraint: Constraint?
    
    var statusVM: NBStatusViewModel? {
        didSet{
            originView.statusVM = statusVM
            resweetedView.statusVM = statusVM
            toolBarView.statusViewModel = statusVM
            
            toolBarTopConstraint?.uninstall()
            if statusVM?.status?.retweeted_status == nil {
                resweetedView.hidden = true
                toolBarView.snp_updateConstraints(closure: { (make) in
                    self.toolBarTopConstraint = make.top.equalTo(originView.snp_bottom).constraint
                })
            }else{
                resweetedView.hidden = false
                toolBarView.snp_updateConstraints(closure: { (make) in
                    self.toolBarTopConstraint = make.top.equalTo(resweetedView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .None
        updateUI()
    }
    
    private func updateUI(){
        contentView.addSubview(originView)
        contentView.addSubview(toolBarView)
        contentView.addSubview(resweetedView)
        
        originView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(contentView)
            //设置一个测试的高度
//            make.height.equalTo(100)
        }
        
        resweetedView.snp_makeConstraints { (make) in
            make.top.equalTo(originView.snp_bottom)
            make.left.right.equalTo(contentView)
        }
        
        toolBarView.snp_makeConstraints { (make) in
            toolBarTopConstraint = make.top.equalTo(resweetedView.snp_bottom).offset(CellMargin).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(36)
        }
        
        contentView.snp_makeConstraints { (make) in
            make.bottom.equalTo(toolBarView)
            make.top.left.right.equalTo(self)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
